# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.provinceDao import ProvinceDao
import time
from . import app

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

"""
表名：zzh_province

说明：省份表
"""

@app.route('/queryProvinceById', methods=['POST'])
def queryProvinceById():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count,userCodeJSON= ProvinceDao().queryProvinceById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryProvince OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryProvinceByCondition', methods=['POST'])
def queryProvinceByCondition():

    if not request.json:
        abort(400)
    provinceName = request.json.get("provinceName")
    pageIndex = request.json.get("pageIndex",1)#传递页面索引号，默认为1
    pageSize = request.json.get("pageSize",10)
    try:
        total_count, UserCodeJSON = ProvinceDao().queryProvinceByCondition(provinceName=provinceName,pageIndex=pageIndex,pageSize=pageSize)

        if total_count:
                return jsonify({"message": {"code": 1, "status": "queryProvince OK"},"total":total_count,"info":UserCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 1, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/insertProvince', methods=['POST'])
def insertProvince():

    if not request.json:
        abort(400)

    Info = {

        'provinceName': request.json.get('provinceName')  #




    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = ProvinceDao().insertProvince(Info)
        if rs:
            return jsonify(
                {"massage": {"code": 1, "info": "addProvince OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
        else:
            return jsonify({"message": {"code": 0, "info": "addProvince Error"}})  # 添加函数调用失败
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/updateProvince', methods=['POST'])
def updateProvince():#

    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    try:
        oldInfo = ProvinceDao().selectProvinceCar(id)
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

    Info = {
         'provinceName': request.json.get('provinceName',oldInfo[1]),  #
         'id': request.json.get('id')  #

    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = ProvinceDao().updateProvince(Info)
        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateProvince OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateProvince Error"}})  # 添加函数调用失败
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteProvince', methods=['POST'])  # 根据id数组删除zzh_user_Code表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteProvince():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = ProvinceDao().deleteProvince(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "deleteProvince OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "deleteProvince Error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})


